package cn.silver.module.configuration.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.silver.framework.excel.utils.ExcelUtil;
import cn.silver.framework.log.annotation.Log;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.mybatis.controller.BusController;
import cn.silver.framework.openapi.model.Result;
import cn.silver.module.configuration.bean.DictBean;
import cn.silver.module.configuration.domain.SysDict;
import cn.silver.module.configuration.param.DictParam;
import cn.silver.module.configuration.service.ISysDictService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author Silver Zhou
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@Tag(name = "配置管理-数据字典信息")
@RequestMapping("/config/dict")
public class SysDictController extends BusController<ISysDictService, SysDict, DictParam, DictBean> {

    @Override
    @Resource
    public void setBaseService(ISysDictService baseService) {
        this.setCode("config:dict");
        this.setTitle("配置管理-数据字典信息");
        this.baseService = baseService;
    }

    /**
     * 导出字典类型列表
     */
    @Log(title = "配置管理-数据字典信息", operate = OperateEnums.OSS_EXPORT)
    @SaCheckPermission("system:dict:export")
    @PostMapping("/export")
    public void export(DictParam dictType, HttpServletResponse response) {
        List<DictBean> list = this.baseService.selectList(dictType);
        ExcelUtil.exportExcel(list, "字典类型", DictBean.class, response);
    }

    /**
     * 刷新字典缓存
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "配置管理-数据字典信息", operate = OperateEnums.ALTER_CLEAR)
    @DeleteMapping("/clear")
    public Result<Void> refreshCache() {
        this.baseService.clearCache();
        return Result.ok();
    }

}
